package com.icloud.admin.resource;

import com.github.pagehelper.PageInfo;
import com.icloud.admin.application.command.AssignMenuToRoleCommand;
import com.icloud.admin.application.AccessApplicationService;
import com.icloud.admin.application.command.AssignResourceToRoleCommand;
import com.icloud.admin.application.command.ChangeRolePopsCommand;
import com.icloud.admin.application.command.CreateRoleCommand;
import com.icloud.admin.query.AccessQueryService;
import com.icloud.admin.query.dto.MenuDTO;
import com.icloud.admin.query.dto.ResourceDTO;
import com.icloud.admin.query.dto.RoleDTO;
import com.icloud.common.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private AccessApplicationService accessApplicationService;

    @Autowired
    private AccessQueryService accessQueryService;

    /**
     * 查询角色列表
     *
     * @param pageNum  页码
     * @param pageSize 页码记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public InvokeResult findRolePaging(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return new InvokeResult(accessQueryService.findRolePaging(pageNum, pageSize));
    }

    /**
     * 创建角色
     *
     * @param createRoleCommand 创建角色命令
     */
    @RequestMapping(method = RequestMethod.POST)
    public InvokeResult createRole(@RequestBody CreateRoleCommand createRoleCommand) {
        accessApplicationService.createRole(createRoleCommand);
        return new InvokeResult();
    }

    /**
     * 删除角色
     *
     * @param roleId 角色Id
     */
    @RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
    public InvokeResult removeRole(@PathVariable("roleId") String roleId) {
        accessApplicationService.removeRole(roleId);
        return new InvokeResult();
    }

    /**
     * 更新角色
     */
    @RequestMapping(value = "/{roleId}", method = RequestMethod.PUT)
    public InvokeResult updateRolePops(@PathVariable("roleId") String roleId, @Valid @RequestBody ChangeRolePopsCommand changeRolePopsCommand) {
        changeRolePopsCommand.setRoleId(roleId);
        accessApplicationService.changeRolePops(changeRolePopsCommand);
        return new InvokeResult();
    }

    /**
     * 获取角色菜单，标注分配和未分配，用于为菜单分配权限
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{roleId}/menus", method = RequestMethod.GET)
    public InvokeResult findeMenusByRoleId(@PathVariable("roleId") String roleId) {
        return new InvokeResult(accessQueryService.findeMenusByRoleId(roleId));
    }

    /**
     * 获取角色菜单
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{roleId}/menus/assigned", method = RequestMethod.GET)
    public InvokeResult findMenusAssignedByRoleId(@PathVariable("roleId") String roleId) {
        return new InvokeResult(accessQueryService.findMenusAssignedByRoleId(roleId));
    }


    @RequestMapping(value = "/{roleId}/menus", method = RequestMethod.PUT)
    public InvokeResult assignMenuToRole(@PathVariable("roleId") String roleId, @RequestBody AssignMenuToRoleCommand assignMenuToRoleCommand) {
        assignMenuToRoleCommand.setRoleId(roleId);
        accessApplicationService.assignMenuToRole(assignMenuToRoleCommand);
        return new InvokeResult();
    }

    /**
     * 获取角色菜单，标注分配和未分配，用于为菜单分配权限
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/{roleId}/resources", method = RequestMethod.GET)
    public InvokeResult findResourceByRoleId(@PathVariable("roleId") String roleId) {
        return new InvokeResult(accessQueryService.findResourceByRoleId(roleId));
    }

    /**
     * 为角色分配资源
     *
     * @param roleId
     * @param assignResourceToRoleCommand
     */
    @RequestMapping(value = "/{roleId}/resources", method = RequestMethod.PUT)
    public InvokeResult assignAccessUrlToRole(@PathVariable("roleId") String roleId, @Valid @RequestBody AssignResourceToRoleCommand assignResourceToRoleCommand) {
        assignResourceToRoleCommand.setRoleId(roleId);
        accessApplicationService.assignResourceToRole(assignResourceToRoleCommand);
        return new InvokeResult();
    }


}
